﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ReceiptTracker
{
    public partial class FormViewMonthly : Form
    {
        private SQLiteDB db;
        private List<Category> categoryList = new List<Category>();
        private PieChart chart;
        private DataTable dt = new DataTable();
        public FormViewMonthly(SQLiteDB db)
        {
            InitializeComponent();
            chart = new PieChart(this.CreateGraphics(), "", "kr");
            this.db = db;
            db.SQLQuery("SELECT MIN(date) AS date FROM Receipts;");
            int minYear;
            try
            {
                minYear = Convert.ToInt32(db.QueryResult.Rows[0]["date"].ToString().Substring(0, 4));
            }
            catch 
            {
                minYear = DateTime.Today.Year;
            }
            for (int i = minYear; i <= DateTime.Today.Year; i++)
            {
                comboBoxYear.Items.Add(i.ToString());                
            }
            comboBoxYear.SelectedIndex = comboBoxYear.Items.Count - 1;

            this.categoryList.Clear();
            db.SQLQuery("SELECT * FROM Categories;");
            foreach (DataRow row in db.QueryResult.Rows)
            {
                categoryList.Add(new Category(Convert.ToInt32(row["id"]), row["name"].ToString()));
            }
            comboBoxCategory.DataSource = categoryList;
            comboBoxCategory.ValueMember = "name";
            comboBoxMonth.DataSource = new string[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
            comboBoxMonth.SelectedIndex = 0;
            GetExpenditure();
            dataGridView.DataSource = dt;
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            if (comboBoxCategory.SelectedIndex != -1)
            {
                db.SQLQuery("SELECT ROUND(SUM(spent),2) AS spent,SUBSTR(date,1,7) AS month FROM Receipts,Shops,Categories WHERE Receipts.shop=Shops.id AND Shops.category=Categories.id AND " +
                            "date>='" + comboBoxYear.SelectedItem + "-01' AND date<='" + comboBoxYear.SelectedItem + "-12' AND Categories.id=" +
                            categoryList[comboBoxCategory.SelectedIndex].Id + " GROUP BY month");
                dt.Clear();
                dt.Merge(db.QueryResult);
            }
        }

        private void GetExpenditure()
        {
            db.SQLQuery("SELECT ROUND(SUM(spent),2) AS spent,SUBSTR(date,1,7) AS month,Categories.name AS category FROM Receipts,Shops,Categories WHERE Receipts.shop=Shops.id AND " +
                "Shops.category=Categories.id AND month='" + comboBoxYear.SelectedItem + "-" + (comboBoxMonth.SelectedIndex + 1).ToString("00") + "' GROUP BY month,category");
            chart.ClearItems();
            foreach (DataRow row in db.QueryResult.Rows)
            {
                chart.AddItem(Convert.ToDouble(row["spent"]), row["category"].ToString());
            }
        }

        private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetExpenditure();
            this.Invalidate();
        }

        private void FormViewMonthly_Paint(object sender, PaintEventArgs e)
        {
            chart.DrawChart(350, 45, 200, 200);
        }
    }
}
